import {duties} from "@/data/constants/duties";
import {dutiesMap} from "@/data/constants/dutiesMap";
import {DutyTypeConstants} from "@/data/constants/Constants";
import {IconUrl} from "@/data/constants/IconsUrl";
import {DutyInfo} from "@/data/entity/DutyInfo";

let dutyNames:string[]|null

export function getDuties(queryString:string) {
  let queryList = queryString.split(' ');

  return queryList.reduce((pre, query) => {
    return pre.filter(item => {
      return item.tags.toLowerCase()
          .indexOf(query.toLowerCase()) > -1
    })
  }, duties.value)
}

export function checkDutyName(name:string):boolean{
  if (dutyNames == null){
    dutyNames = duties.value.map(({value}) => value)
  }
  return dutyNames.indexOf(name) > -1;
}

export function getDutyInfo(dutyName:string):DutyInfo{
  return DutyInfo.parse(dutiesMap.get(dutyName))
}

export function getDutyIconUrl(dutyTypeOrName:number|string):string{
  if (typeof dutyTypeOrName == "string"){
    const duty = dutiesMap.get(dutyTypeOrName)
    if (duty){
      dutyTypeOrName = duty.type
    } else {
      dutyTypeOrName = DutyTypeConstants.Roulette
    }
  }
  switch (dutyTypeOrName) {
    case DutyTypeConstants.Roulette:
      return IconUrl.duty.Roulette
    case DutyTypeConstants.Dungeon:
      return IconUrl.duty.Dungeon
    case DutyTypeConstants.Raid:
      return IconUrl.duty.Raid
    case DutyTypeConstants.Guildhest:
      return IconUrl.duty.Guildhest
    case DutyTypeConstants.Trial:
      return IconUrl.duty.Trial
    default:
      return IconUrl.duty.Roulette
  }
}

